package com.thinkinghead.mahjongrecorder;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.LogInCallback;
import com.parse.ParseAnalytics;
import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;
import com.thinkinghead.mahjongrecorder.ui.dialog.DialogHelper;
import com.thinkinghead.mahjongrecorder.utils.TypeFaceManager;

public class LoginActivity extends Activity {
	private static final String TAG = "LoginActivity";
	// Declare Variables
	Button loginbutton,signup;
	String usernametxt,passwordtxt;
	EditText password,username;
	TextView view_username, view_password;
	ParseUser user;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_signup);
		fnInitParse();
		fnInitView();
		initFontFace();
		fnCheckUser();
		fnTitle();
	}
	
	private void fnTitle() {
		ActionBar actionBar = getActionBar();
		actionBar.setTitle("Please Login / Signup");
	}
	
	private void fnInitView() {
		view_username = (TextView) findViewById(R.id.tv_username);
		view_password = (TextView) findViewById(R.id.tv_password);

		username = (EditText) findViewById(R.id.txt_username);
		password = (EditText) findViewById(R.id.txt_password);

		loginbutton = (Button) findViewById(R.id.btn_login);
		signup = (Button) findViewById(R.id.btn_signup);
	}
	
	private void initFontFace() {
		Typeface tf = TypeFaceManager.getTypeFace(getAssets());
		if(tf!=null){
			view_username.setTypeface(tf);
			view_password.setTypeface(tf);
			loginbutton.setTypeface(tf);
			signup.setTypeface(tf);			
		}
	}
	
	private void fnInitParse() {
		ParseAnalytics.trackAppOpened(getIntent());
		user = ParseUser.getCurrentUser();
	}

	public void fnLogin(View arg0) {
		// Retrieve the text entered from the EditText
		usernametxt = username.getText().toString();
		passwordtxt = password.getText().toString();
		
		fnPreSave();
		final LoginActivity ctx = this;
		// Send data to Parse.com for verification
		ParseUser.logInInBackground(usernametxt, passwordtxt,
				new LogInCallback() {
					public void done(ParseUser parseUser, ParseException e) {
						fnPostSave();
						if (parseUser != null) {
							// If user exist and authenticated
							user = parseUser;
							fnPostLogin();
						} else {
							DialogHelper.getOkAlertDialog(ctx, "Error in connecting server..", e.getMessage()).show();
						}
					}
				});
	}

	public void fnSignUp(View arg0) {
		// Retrieve the text entered from the EditText
		usernametxt = username.getText().toString();
		passwordtxt = password.getText().toString();

		// Force user to fill up the form
		if (usernametxt.equals("") || passwordtxt.equals("")) {
			Toast.makeText(getApplicationContext(),
					"Please complete the sign up form", Toast.LENGTH_LONG)
					.show();

		} else {
			final LoginActivity ctx = this;
			fnPreSave();
			// Save new user data into Parse.com Data Storage
			final ParseUser puser = new ParseUser();
			puser.setUsername(usernametxt);
			puser.setPassword(passwordtxt);
			puser.signUpInBackground(new SignUpCallback() {
				public void done(ParseException e) {
					fnPostSave();
					if (e == null) {
						// Show a simple Toast message upon successful
						// registration
						Toast.makeText(getApplicationContext(),
								"Successfully Signed up, please log in.",
								Toast.LENGTH_LONG).show();
						
						user = puser;
						fnPostLogin();
					} else {
						DialogHelper.getOkAlertDialog(ctx, "Error in connecting server..", e.getMessage()).show();
					}
				}
			});
		}

	}

	private void fnCheckUser() {
		if (user != null && user.getObjectId() != null) {
			fnPostLogin();
			return;
		}
	}

	private void fnPostLogin() {
		Log.d(TAG, "Login..." + ParseUser.getCurrentUser().getObjectId());
		Intent intent = new Intent(LoginActivity.this, GameListActivity.class);
		startActivity(intent);
		Toast.makeText(getApplicationContext(), "Successfully Logged in",
				Toast.LENGTH_LONG).show();
		finish();
	}
	
	private void fnPreSave() {
		DialogHelper.fnShowDialog(this);
	}
	
	private void fnPostSave() {
		DialogHelper.fnCloseDialog();
	}
}
